﻿
using System;
using System.Text;
using System.Web;

namespace OpusSuite.Utility
{
    public static class WebUtil
    {
        public static void RemoveAuthCookie()
        {
            HttpContext.Current.Response.Cookies.Remove("OpusSuiteAuth");

            if (HttpContext.Current.Request.Cookies["OpusSuiteAuth"] != null)
            {
                HttpCookie cookie = new HttpCookie("OpusSuiteAuth") {Expires = DateTime.Now.AddDays(-1)};
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }

        public static HttpCookie CookieFromLogin(string userName, string password, string databaseName, string serverName, Enums.ServerTypes serverType)
        {
            var cookie = new HttpCookie("OpusSuiteAuth")
                             {
                                 Value =
                                     userName + ";" + password + ";" + databaseName + ";" + serverName + ";" +
                                     serverType
                             };
            cookie.Value = CryptoUtil.EncryptStringAES(cookie.Value, ConfigUtil.AESKey());

            return cookie;
        }

        public static string ConnectionStringFromCookie()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["OpusSuiteAuth"];

            if (cookie != null)
            {
                var s = CryptoUtil.DecryptStringAES(cookie.Value, ConfigUtil.AESKey());
                var sItems = s.Split(';');

                switch ((Enums.ServerTypes)Enum.Parse(typeof(Enums.ServerTypes), sItems[4]))
                {
                    case Enums.ServerTypes.SqlServer:
                        return "Data Source=" + sItems[3] + ";Initial Catalog=" + sItems[2] + ";User ID=" + sItems[0] + ";Password=" + sItems[1];

                    case Enums.ServerTypes.MySqlServer:
                        return "Server=" + sItems[3] + ";Database=" + sItems[2] + ";Uid=" + sItems[0] + ";Pwd=" + sItems[1];
                } 
            }

            return "";
        }

        public static string ConnectionStringFromLogin(string userName, string password, string databaseName, string serverName, Enums.ServerTypes serverType)
        {
            // TODO, move into IOpusProvider or similar
            switch (serverType)
            {
                case Enums.ServerTypes.SqlServer:
                    return "Data Source=" + serverName + ";Initial Catalog=" + databaseName + ";User ID=" + userName + ";Password=" + password;

                case Enums.ServerTypes.MySqlServer:
                    return "Server=" + serverName + ";Database=" + databaseName + ";Uid=" + userName + ";Pwd=" + password;
            }

            return "";
        }

        public static Enums.ServerTypes ServerTypeFromCookie()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["OpusSuiteAuth"];

            if (cookie != null)
            {
                var s = CryptoUtil.DecryptStringAES(cookie.Value, ConfigUtil.AESKey());
                var sItems = s.Split(';');
                return (Enums.ServerTypes)Enum.Parse(typeof(Enums.ServerTypes), sItems[4]);
            }

            return Enums.ServerTypes.MySqlServer;
        }

        public static string UserNameFromCookie()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["OpusSuiteAuth"];

            if (cookie != null)
            {
                var s = CryptoUtil.DecryptStringAES(cookie.Value, ConfigUtil.AESKey());
                var sItems = s.Split(';');
                return sItems[0];
            }

            return "";
        }

        public static string DatabaseNameFromCookie()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["OpusSuiteAuth"];

            if (cookie != null)
            {
                var s = CryptoUtil.DecryptStringAES(cookie.Value, ConfigUtil.AESKey());
                var sItems = s.Split(';');
                return sItems[2];
            }

            return "";
        }

        public static string ServerNameFromCookie()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["OpusSuiteAuth"];

            if (cookie != null)
            {
                var s = CryptoUtil.DecryptStringAES(cookie.Value, ConfigUtil.AESKey());
                var sItems = s.Split(';');
                return sItems[3];
            }

            return "";
        }
        
        public static void AddThemeCookie(string theme)
        {
            //System.Web.HttpContext.Current.Response.Cookies.Remove("OpusSuiteAuth");

            if (HttpContext.Current.Request.Cookies["OpusSuiteTheme"] != null)
            {
                HttpCookie cookie = new HttpCookie("OpusSuiteTheme") {Value = theme};
                HttpContext.Current.Request.Cookies.Add(cookie);
            }
        }

        public static string ThemeCookie()
        {
            //System.Web.HttpContext.Current.Response.Cookies.Remove("OpusSuiteAuth");

            if (HttpContext.Current.Request.Cookies["OpusSuiteTheme"] != null)
            {
// ReSharper disable PossibleNullReferenceException
                return HttpContext.Current.Request.Cookies["OpusSuiteTheme"].Value;
// ReSharper restore PossibleNullReferenceException
            }

            return "";
        }

        public static string BuildBreadcrumbs(string crumbs)
        {
            StringBuilder s = new StringBuilder();

            var split = crumbs.Split(',');
            foreach (var crumb in split)
            {
                switch(crumb.ToLower())
                {
                    case "home":
                        //s.AppendFormat("<a href=\"/.")
                        break;
                }
            }

            return s.ToString();
        }
    }
}
